API: Add support for siblings to widget paths
authorBenjamin Otte <otte@redhat.com>
Fri, 27 May 2011 16:22:09 +0000 (18:22 +0200)
committerBenjamin Otte <otte@redhat.com>
Thu, 2 Jun 2011 00:03:51 +0000 (02:03 +0200)
docs/reference/gtk/gtk3-sections.txt
gtk/gtk.symbols
gtk/gtkwidgetpath.c
gtk/gtkwidgetpath.h

index 45f17734bd1990f5a1b09065b0e51ef7d3be60e5..56e9ade89630330c505b5fdeed122dc7e7f5a65c 100644 (file)
@@ -5416,6 +5416,7 @@ GTK_CHECK_VERSION
 <TITLE>GtkWidgetPath</TITLE>
 GtkWidgetPath
 gtk_widget_path_append_type
+gtk_widget_path_append_with_siblings
 gtk_widget_path_copy
 gtk_widget_path_ref
 gtk_widget_path_unref
@@ -5429,6 +5430,8 @@ gtk_widget_path_iter_clear_classes
 gtk_widget_path_iter_clear_regions
 gtk_widget_path_iter_get_name
 gtk_widget_path_iter_get_object_type
+gtk_widget_path_iter_get_siblings
+gtk_widget_path_iter_get_sibling_index
 gtk_widget_path_iter_has_class
 gtk_widget_path_iter_has_name
 gtk_widget_path_iter_has_qclass
index 4948e92d45fe711a4b8fcda868bea48bc83c7bfb..90eae718e5355cd3fba1bfa8fee51e913e45a42b 100644 (file)
@@ -3577,6 +3577,7 @@ gtk_widget_override_font
 gtk_widget_override_symbolic_color
 gtk_widget_path
 gtk_widget_path_append_type
+gtk_widget_path_append_with_siblings
 gtk_widget_path_copy
 gtk_widget_path_ref
 gtk_widget_path_unref
@@ -3591,6 +3592,8 @@ gtk_widget_path_iter_clear_classes
 gtk_widget_path_iter_clear_regions
 gtk_widget_path_iter_get_name
 gtk_widget_path_iter_get_object_type
+gtk_widget_path_iter_get_siblings
+gtk_widget_path_iter_get_sibling_index
 gtk_widget_path_iter_has_class
 gtk_widget_path_iter_has_name
 gtk_widget_path_iter_has_qclass
index 0d878f528bdcdb7149b71bfb812f3b5b61cb5c45..c6f3fb3c1b11357f99aaa8ae77606836fa5a409c 100644 (file)
@@ -93,6 +93,8 @@ struct GtkPathElement
   GQuark name;
   GHashTable *regions;
   GArray *classes;
+  GtkWidgetPath *siblings;
+  guint sibling_index;
 };
 
 struct _GtkWidgetPath
@@ -131,6 +133,9 @@ gtk_path_element_copy (GtkPathElement       *dest,
 
   dest->type = src->type;
   dest->name = src->name;
+  if (src->siblings)
+    dest->siblings = gtk_widget_path_ref (src->siblings);
+  dest->sibling_index = src->sibling_index;
 
   if (src->regions)
     {
@@ -235,6 +240,9 @@ gtk_widget_path_unref (GtkWidgetPath *path)
 
       if (elem->classes)
         g_array_free (elem->classes, TRUE);
+
+      if (elem->siblings)
+        gtk_widget_path_unref (elem->siblings);
     }
 
   g_array_free (path->elems, TRUE);
@@ -409,6 +417,100 @@ gtk_widget_path_append_type (GtkWidgetPath *path,
   return path->elems->len - 1;
 }
 
+/**
+ * gtk_widget_path_append_with_siblings:
+ * @path: the widget path to append to
+ * @siblings: a widget path describing a list of siblings. This path
+ *   may not contain any siblings itself and it must not be modified
+ *   afterwards.
+ * @sibling_index: index into @siblings for where the added element is
+ *   positioned.
+ *
+ * Appends a widget type with all its siblings to the widget hierarchy
+ * represented by @path. Using this function instead of
+ * gtk_widget_path_append_type() will allow the CSS theming to use
+ * sibling matches in selectors and apply :nth-child() pseudo classes.
+ * In turn, it requires a lot more care in widget implementations as
+ * widgets need to make sure to call gtk_widget_reset_style() on all
+ * involved widgets when the @siblings path changes.
+ *
+ * Returns: the position where the element was inserted.
+ *
+ * Since: 3.2
+ **/
+gint
+gtk_widget_path_append_with_siblings (GtkWidgetPath *path,
+                                      GtkWidgetPath *siblings,
+                                      guint          sibling_index)
+{
+  GtkPathElement new;
+
+  g_return_val_if_fail (path != NULL, 0);
+  g_return_val_if_fail (siblings != NULL, 0);
+  g_return_val_if_fail (sibling_index < gtk_widget_path_length (siblings), 0);
+
+  gtk_path_element_copy (&new, &g_array_index (siblings->elems, GtkPathElement, sibling_index));
+  new.siblings = gtk_widget_path_ref (siblings);
+  new.sibling_index = sibling_index;
+  g_array_append_val (path->elems, new);
+
+  return path->elems->len - 1;
+}
+
+/**
+ * gtk_widget_path_iter_get_siblings:
+ * @path: a #GtkWidgetPath
+ * @pos: position to get the siblings for, -1 for the path head
+ *
+ * Returns the list of siblings for the element at @pos. If the element
+ * was not added with siblings, %NULL is returned.
+ *
+ * Returns: %NULL or the list of siblings for the element at @pos.
+ **/
+const GtkWidgetPath *
+gtk_widget_path_iter_get_siblings (const GtkWidgetPath *path,
+                                   gint                 pos)
+{
+  GtkPathElement *elem;
+
+  g_return_val_if_fail (path != NULL, G_TYPE_INVALID);
+  g_return_val_if_fail (path->elems->len != 0, G_TYPE_INVALID);
+
+  if (pos < 0 || pos >= path->elems->len)
+    pos = path->elems->len - 1;
+
+  elem = &g_array_index (path->elems, GtkPathElement, pos);
+  return elem->siblings;
+}
+
+/**
+ * gtk_widget_path_iter_get_sibling_index:
+ * @path: a #GtkWidgetPath
+ * @pos: position to get the sibling index for, -1 for the path head
+ *
+ * Returns the index into the list of siblings for the element at @pos as
+ * returned by gtk_widget_path_iter_get_siblings(). If that function would
+ * return %NULL because the element at @pos has no siblings, this function
+ * will return 0.
+ *
+ * Returns: 0 or the index into the list of siblings for the element at @pos.
+ **/
+guint
+gtk_widget_path_iter_get_sibling_index (const GtkWidgetPath *path,
+                                        gint                 pos)
+{
+  GtkPathElement *elem;
+
+  g_return_val_if_fail (path != NULL, G_TYPE_INVALID);
+  g_return_val_if_fail (path->elems->len != 0, G_TYPE_INVALID);
+
+  if (pos < 0 || pos >= path->elems->len)
+    pos = path->elems->len - 1;
+
+  elem = &g_array_index (path->elems, GtkPathElement, pos);
+  return elem->sibling_index;
+}
+
 /**
  * gtk_widget_path_iter_get_object_type:
  * @path: a #GtkWidgetPath
index 12822a8c0d755c46781e688c4161c75c5b7b6fae..66441fa3a14be3b3189ab04f1a3cf7ae4c40ad66 100644 (file)
@@ -52,15 +52,23 @@ gint            gtk_widget_path_append_type         (GtkWidgetPath       *path,
                                                      GType                type);
 void            gtk_widget_path_prepend_type        (GtkWidgetPath       *path,
                                                      GType                type);
+gint            gtk_widget_path_append_with_siblings(GtkWidgetPath       *path,
+                                                     GtkWidgetPath       *siblings,
+                                                     guint                sibling_index);
 /* gtk_widget_path_append_for_widget() is declared in gtkwidget.c */
 gint            gtk_widget_path_append_for_widget   (GtkWidgetPath       *path,
                                                      GtkWidget           *widget);
 
-GType               gtk_widget_path_iter_get_object_type (const GtkWidgetPath *path,
-                                                          gint                 pos);
-void                gtk_widget_path_iter_set_object_type (GtkWidgetPath       *path,
-                                                          gint                 pos,
-                                                          GType                type);
+GType               gtk_widget_path_iter_get_object_type  (const GtkWidgetPath *path,
+                                                           gint                 pos);
+void                gtk_widget_path_iter_set_object_type  (GtkWidgetPath       *path,
+                                                           gint                 pos,
+                                                           GType                type);
+const GtkWidgetPath *
+                    gtk_widget_path_iter_get_siblings     (const GtkWidgetPath *path,
+                                                           gint                 pos);
+guint               gtk_widget_path_iter_get_sibling_index(const GtkWidgetPath *path,
+                                                           gint                 pos);
 
 G_CONST_RETURN gchar * gtk_widget_path_iter_get_name  (const GtkWidgetPath *path,
                                                        gint                 pos);